package com.sxt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.sxt.common.model.User;
import com.sxt.service.UserService;


/**
 * 
* Title: UserServiceImpl
* Description:
* 用户操作实现类 
* Version:1.0.0  
* @author luolin
* @date 2020年1月2日
 */
@Service
public class UserServiceImpl implements UserService {
	static List<User> userList;
	//初始化数据
	static {
		userList = new ArrayList<>();
		User user1 = new User();
		user1.setId(0);
		user1.setUserCode("user_001");
		user1.setUserName("张三");
		user1.setUserPwd("12345");
		user1.setCreateBy("admin");
		user1.setCreateDate(new Date());
		user1.setSortNo(0);
		
		User user2 = new User();
		user2.setId(1);
		user2.setUserCode("user_002");
		user2.setUserName("李四");
		user2.setUserPwd("12345");
		user2.setCreateBy("admin");
		user2.setCreateDate(new Date());
		user2.setSortNo(1);
		
		
		User user3 = new User();
		user3.setId(2);
		user3.setUserCode("user_003");
		user3.setUserName("王五");
		user3.setUserPwd("12345");
		user3.setCreateBy("admin");
		user3.setCreateDate(new Date());
		user3.setSortNo(2);
		
		User user4 = new User();
		user4.setId(3);
		user4.setUserCode("user_004");
		user4.setUserName("刘德华");
		user4.setUserPwd("12345");
		user4.setCreateBy("admin");
		user4.setCreateDate(new Date());
		user4.setSortNo(3);
		
		
		User user5 = new User();
		user5.setId(4);
		user5.setUserCode("user_005");
		user5.setUserName("郭富城");
		user5.setUserPwd("12345");
		user5.setCreateBy("admin");
		user5.setCreateDate(new Date());
		user5.setSortNo(4);
		
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);
		userList.add(user4);
		userList.add(user5);
	}
	
	@Override
	public boolean addUser(User user) {
		return userList.add(user);
	}

	@Override
	public boolean updateUser(User user) {
		User rs = this.findUserById(user.getId());
		userList.remove(rs);
		return this.addUser(user);
	}

	@Override
	public boolean deleteUser(int id) {
		boolean rs = false;
		for(User user: userList) {
			if(id == user.getId()) {
				rs = userList.remove(user);
			}
			
		}
		// TODO Auto-generated method stub
		return rs;
	}

	@Override
	public User findUserByName(String userName) {
		// TODO Auto-generated method stub
		for(User user: userList) {
			if(userName.equals(user.getUserName()) ) {
				return user;
			}
			
		}
		return null;
	}

	@Override
	public User findUserById(long id) {
		for(User user: userList) {
			if(id == user.getId()) {
				return user;
				
			}
		}
		return null;
	}

	@Override
	public List<User> findUserAll() {
		// TODO Auto-generated method stub
		return userList;
	}

}
